<?php
class Zendvn_Models_Pages extends Zend_Db_Table{
	protected $_name = 'home';
	protected $_primary = 'id';
	public function listItem($arrParam = null, $options = null){										
		$db = $this->getAdapter();
		if($options['task'] == 'admin-list'){
			$select = $db->select()
						 ->from($this->_name,array('id','title','page','description','contact','description_html','keywords_html','color','status'));
			$result  = $db->fetchAll($select);
		}
		if($options['task'] == 'pulic-home'){
			$select = $db->select()
						 ->from($this->_name,array('id','title','page','description','contact','description_html','keywords_html','color','status'))
						 ->where('status = 1')
						 ->where('page = "home"');
			$result  = $db->fetchAll($select);
		}
		if($options['task'] == 'contact'){
			$select = $db->select()
						 ->from($this->_name,array('id','title','page','description','contact','description_html','keywords_html','color','status'))
						 ->where('status = 1')
						 ->where('page = "home"');
			$result  = $db->fetchRow($select);
		}
		return $result;
	}
	
	public function saveItem($arrParam = null, $options = null){
		if($options['task'] == 'admin-add'){
			$row =  $this->fetchNew();
		}
		if($options['task'] == 'admin-edit'){
			$where = 'id = ' . $arrParam['id'];			
			$row =  $this->fetchRow($where);
		}
		$row->title 				= $arrParam['title'];
		$row->page 					= $arrParam['page'];
		$row->status 				= $arrParam['status'];
		$row->description_html 		= $arrParam['description_html'];
		$row->keywords_html 		= $arrParam['keywords_html'];
		$row->contact 				= $arrParam['contact'];
		$row->description 			= $arrParam['description'];
		$row->top_left 			= $arrParam['top_left'];
		$row->top_right 			= $arrParam['top_right'];
		$row->address 			= $arrParam['address'];
		$row->phone 			= $arrParam['phone'];
		$row->mail 			= $arrParam['mail'];
		$row->contact 				= $arrParam['contact'];
		$row->save();		
	}
	public function getItem($arrParam = null, $options = null){		
		if($options['task'] == 'admin-edit'){
			$where = 'id = ' . $arrParam['id'];
			$result = $this->fetchRow($where)->toArray();
		}
		if($options['task'] == 'public-home'){
			$where = 'page = "home"';
			$result = $this->fetchRow($where)->toArray();
		}
		if($options['task'] == 'public-news'){
			$where = 'page = "news"';
			$result = $this->fetchRow($where)->toArray();
		}
		if($options['task'] == 'public-tour'){
			$where = 'page = "tour"';
			$result = $this->fetchRow($where)->toArray();
		}
		if($options['task'] == 'public-service'){
			$where = 'page = "service"';
			$result = $this->fetchRow($where)->toArray();
		}
		if($options['task'] == 'public-photos'){
			$where = 'page = "photos"';
			$result = $this->fetchRow($where)->toArray();
		}
		return $result;
	}
	
	public function deleteItem($arrParam = null, $options = null){
		if($options['task'] == 'admin-delete'){
			$where = ' id = ' . $arrParam['id'];
			$this->delete($where);
		}		
		if($options['task'] == 'admin-multi-delete'){
			$cid = $arrParam['cid'];
			if(count($cid)>0){				
				$ids = implode(',',$cid);			
				$db = $this->getAdapter();
				$where = 'id IN (' . $ids . ')';
				$this->delete($where);
			}
		}
	}
	
	public function changeStatus($arrParam = null, $options = null){
		$cid = $arrParam['cid'];		
		if(count($cid)>0){
			if($arrParam['type'] == 1){
				$status = 1;
			}else{
				$status = 0;
			}			
			$ids = implode(',',$cid);
			$data = array('status'=>$status);
			$where = 'id IN (' . $ids . ')';
			$this->update($data,$where);
		}
		if($arrParam['id']>0){
			if($arrParam['type'] == 1){
				$status 	= 1;
			}else{
				$status  	= 0;
			}			
			$data = array('status'=>$status);
			$where = 'id = ' . $arrParam['id'];
			$this->update($data,$where);
		}		
	}
	
	
	public function sortItem($arrParam = null, $options = null){
		$items = $arrParam[$options['column']];
		if(count($items)>0){
			foreach ($items as $key => $val) {
				$where = 'id = ' . $key;				
				$data = array($options['column']=>$val);
				$this->update($data,$where);
			}
		}
	}
}